const koa = require('koa')
const cors = require('koa2-cors')
const { koaBody } = require('koa-body')
const bodyparser = require('koa-bodyparser')
const json = require('koa-json')
const config = require('./config')
const logger = require('./logger')
const router = require('./router')
const httpMiddleware = require('./middleware/http')
const errorMiddleware = require('./middleware/error')

global.logger = logger

const { port } = config

const app = new koa()

app.use(errorMiddleware)

app.use(cors())

app.use(koaBody({
	multipart  : true,
	formidable : {
		maxFileSize : 1024 * 1024 * 1024
	}
}))

app.use(bodyparser({
	enableTypes : [
		'json',
		'form',
		'text'
	]
}))

app.use(json())

app.use(httpMiddleware)

app.use(router.routes(), router.allowedMethods())

app.listen(port)

logger.info(`服务已启动监听端口号 ${port}`)
